﻿using System;
using System.Net;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using System.Windows;

namespace eLearning.Util
{
    public class Utility
    {
        public static bool isNetworkAvailable()
        {
            return NetworkInterface.GetIsNetworkAvailable();
        }

        public static bool networkPing(string address)
        {
            HttpWebRequest http = HttpWebRequest.CreateHttp(address);
            return http.HaveResponse;
        }

        public static bool isNumber(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            else
            {
                foreach (char karakter in value)
                {
                    if (!char.IsNumber(karakter))
                    {
                        return false;
                    }
                }
                return true;
            }
        }

        public static bool isNilaiQuiz(string value)
        {
            value = string.IsNullOrEmpty(value) ? "" : value.ToUpper();
            return "A".Equals(value) || "B".Equals(value) || "C".Equals(value) || "D".Equals(value) || "E".Equals(value);
        }

        public static string nullToEmpty(string value)
        {
            return value == null ? "" : value;
        }

        public static string subString(string value, int start, int end)
        {
            string result = "";
            if (!string.IsNullOrEmpty(value))
            {
                result = value.Length <= end ? value.Substring(start, value.Length) : value.Substring(start, end);
            }
            return result;
        }

        public static int parseInt(string value)
        {
            int num;
            if (int.TryParse(value, out num))
            {
                return num;
            }
            else
            {
                throw new FormatException();
            }
        }

        public static void GotoPage(PhoneApplicationPage current, string page)
        {
            current.NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }

        public static void RemoveAllPreviousPage(PhoneApplicationPage current)
        {
            JournalEntry entry = current.NavigationService.RemoveBackEntry();
            entry.Source = current.NavigationService.Source;
        }
    }
}
